using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class StudentPages_AddClass : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
        
        
    }
    private void BindGrid()
    {
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;

        

        string query = @"
            SELECT        
                Course.course_ID AS 'COURSE_ID', 
                Course.course_Name AS 'COURSE', 
                Course.room_Num AS 'ROOM', 
                Course.credits AS 'CREDITS', 
                Instructor.last_Name AS 'LAST'
            FROM            
                Instructor 
            INNER JOIN
                Course 
            ON 
                Instructor.instructor_ID = Course.instructor_ID";

        string connectionString = ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;

        conn = new SqlConnection(connectionString);
        comm = new SqlCommand(query, conn);

        try
        {
            conn.Open();

            reader = comm.ExecuteReader();
            courseGrid.DataSource = reader;
            
            courseGrid.DataKeyNames = new string[] { "COURSE_ID" };
            courseGrid.DataBind();

            reader.Close();
        }
        finally
        {
            comm = null;
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
            conn = null;
        }
    }


    protected void courseGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        UpdateRoster();
    }

    private void UpdateRoster()
    {
        SqlConnection conn;
        SqlCommand comm;

        string query = @"
                INSERT INTO
                    Roster 
                    (student_ID,
                    course_ID)
                VALUES
                    (@student,
                    @course)";

        string connectionString = ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;
        conn = new SqlConnection(connectionString);
        comm = new SqlCommand(query, conn);
        
        try
        {
            

            int selectedRow = Convert.ToInt32(courseGrid.SelectedValue.ToString()) - 1;

            string id = (string)(Session["ID"]);
            int courseID = Convert.ToInt32(courseGrid.DataKeys[selectedRow].Value);

            int studentID = Convert.ToInt32(id);

            comm.Parameters.AddWithValue("@student", studentID);
            comm.Parameters.AddWithValue("@course", courseID);


            conn.Open();
            comm.ExecuteNonQuery();

           
        }
        catch
        {
        }
        finally
        {
            conn.Close();
        }
            

    }

    private int getStudentID()
    {
        string user = User.Identity.Name.ToUpper().Trim().ToString();

        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;

        string connectionString = ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;

        string query = "SELECT student_ID FROM student WHERE username = @user";

        conn = new SqlConnection(connectionString);
        comm = new SqlCommand(query, conn);

        comm.Parameters.AddWithValue("@user", user);

        try
        {
            conn.Open();

            reader = comm.ExecuteReader();

            if (reader["student_ID"] != DBNull.Value)
            {
                int ID = Convert.ToInt32(reader["student_ID"].ToString());
                return ID;
            }
            else
            {
                return 0;
            }
        }
        catch
        {
            return 0;
        }
        finally
        {
            conn.Close();
        }
    }
    
}
